from rest_framework import serializers

from .models import Subject


class SubjectSerializer(serializers.HyperlinkedModelSerializer):
    """
    课题管理
    """
    readable_subject_principal = serializers.SerializerMethodField()
    readable_subject_participant = serializers.SerializerMethodField()

    class Meta:
        model = Subject
        fields = (
            'url',
            'subject_name',
            'subject_type',
            'subject_keyword',
            'subject_principal',
            'subject_participant',
            'readable_subject_principal',
            'readable_subject_participant',
        )

    def get_readable_subject_principal(self, obj):
        return obj.subject_principal.username

    def get_readable_subject_participant(self, obj):
        participants = []
        for participant in obj.subject_participant.all():
            participants.append(participant.username)
        return participants
